//
//  EFAMatAddImgTCell.m
//  efairy
//
//  Created by XiangDongMBP on 2017/9/6.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFAMatAddImgTCell.h"
#import "EFANoticePubCCell.h"
#import "EFAModel.h"

@interface EFAMatAddImgTCell ()<UICollectionViewDelegate, UICollectionViewDataSource, EFANoticePubCCellDelegate>
@property (nonatomic, readonly) NSInteger imgMaxCnt;
@property (nonatomic, readonly) NSArray<EFANotPubItem *> *image_list;
@end

static NSString *const kReuseCellId = @"EFANoticePubCCell";

@implementation EFAMatAddImgTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    UINib *cellNib = [UINib nibWithNibName:kReuseCellId bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:kReuseCellId];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.devModel) {
        return (self.devModel.image_list.count < self.devModel.imgMaxCnt) ? self.devModel.image_list.count + 1 : self.devModel.imgMaxCnt;
    }
    return (self.image_list.count < self.imgMaxCnt) ? self.image_list.count + 1 : self.imgMaxCnt;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    EFANoticePubCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kReuseCellId forIndexPath:indexPath];
    if (self.devModel) {
        EFADevPubItem *item = nil;
        if (indexPath.row < self.devModel.image_list.count) {
            item = self.devModel.image_list[indexPath.row];
        }
        [cell configViewWithDevPubItem:item];
    } else {
        EFANotPubItem *item = nil;
        if (indexPath.row < self.image_list.count) {
            item = self.image_list[indexPath.row];
        }
        [cell configViewWithNotPubItem:item];
    }
    cell.delegate = self;
    return cell;
}

//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.section == 0 && [kind isEqualToString:UICollectionElementKindSectionHeader]) {
//        EFANoticeEditCRView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kReuseCRViewId forIndexPath:indexPath];
//        header.pubModel = self.pubModel;
//        return header;
//    }
//    return nil;
//}

#pragma mark - <UICollectionViewDelegateFlowLayout>
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    return CGSizeMake(CGRectGetWidth(self.view.bounds), 254.0);
//}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*15 - 3 * 10) / 4.0;
    return CGSizeMake(width, width);
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.devModel) {
        if (indexPath.row == self.devModel.image_list.count) {
            if ([self.delegate respondsToSelector:@selector(matAddImgTCell:collectionView:addWithIndexPath:)]) {
                [self.delegate matAddImgTCell:self collectionView:collectionView addWithIndexPath:indexPath];
            }
        }
    } else {
        if (indexPath.row == self.image_list.count) {
            if ([self.delegate respondsToSelector:@selector(matAddImgTCell:collectionView:addWithIndexPath:)]) {
                [self.delegate matAddImgTCell:self collectionView:collectionView addWithIndexPath:indexPath];
            }
        }
    }
}

#pragma mark - EFANoticePubCCellDelegate
- (void)noticePubCCell:(EFANoticePubCCell *)cell closeBtnTapped:(UIButton *)btn {
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    if ([self.delegate respondsToSelector:@selector(matAddImgTCell:collectionView:delWithIndexPath:)]) {
        [self.delegate matAddImgTCell:self collectionView:self.collectionView delWithIndexPath:indexPath];
    }
}

- (void)configViewWithMtcPubModel:(EFAMtcPubModel *)model {
    self.model = model;
    
    NSInteger cnt = (self.image_list.count < self.imgMaxCnt) ? self.image_list.count + 1 : self.imgMaxCnt;
    NSInteger row = cnt / 4 + ((cnt % 4) ? 1 : 0) ;
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*15 - 3 * 10) / 4.0;
    CGFloat cViewHeight = 10 * 2 + width * row + (row - 1) * 10;
    self.collectionViewHeight.constant = cViewHeight;
    
    [self.collectionView reloadData];
}

#pragma mark - getters and setters
- (NSInteger)imgMaxCnt {
    if (self.model) {
        return self.model.imgMaxCnt;
    } else if (self.alarmModel) {
        return self.alarmModel.imgMaxCnt;
    } else {
        return self.dangerModel.imgMaxCnt;
    }
}
- (NSArray<EFANotPubItem *> *)image_list {
    if (self.model) {
        return self.model.image_list;
    } else if (self.alarmModel) {
        return self.alarmModel.image_list;
    } else {
        return self.dangerModel.image_list;
    }
}

@end


@implementation EFAMatAddImgTCell (AlarmRelative)
- (void)configViewWithAlarmPubModel:(EFAAlarmPubModel *)model {
    self.alarmModel = model;
    
    NSInteger cnt = (self.alarmModel.image_list.count < self.alarmModel.imgMaxCnt) ? self.alarmModel.image_list.count + 1 : self.alarmModel.imgMaxCnt;
    NSInteger row = cnt / 4 + ((cnt % 4) ? 1 : 0) ;
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*15 - 3 * 10) / 4.0;
    CGFloat cViewHeight = 10 * 2 + width * row + (row - 1) * 10;
    self.collectionViewHeight.constant = cViewHeight;
    
    [self.collectionView reloadData];
}
@end


@implementation EFAMatAddImgTCell (DangerRelative)
- (void)configViewWithDangerPubModel:(EFADangerPubModel *)model {
    self.dangerModel = model;
    
    NSInteger cnt = (model.image_list.count < model.imgMaxCnt) ? model.image_list.count + 1 : model.imgMaxCnt;
    NSInteger row = cnt / 4 + ((cnt % 4) ? 1 : 0) ;
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*15 - 3 * 10) / 4.0;
    CGFloat cViewHeight = 10 * 2 + width * row + (row - 1) * 10;
    self.collectionViewHeight.constant = cViewHeight;
    
    [self.collectionView reloadData];
}
@end


@implementation EFAMatAddImgTCell (DeviceRelative)
- (void)configViewWithDevPubModel:(EFADevPubModel *)model {
    self.devModel = model;
    
    NSInteger cnt = (model.image_list.count < model.imgMaxCnt) ? model.image_list.count + 1 : model.imgMaxCnt;
    NSInteger row = cnt / 4 + ((cnt % 4) ? 1 : 0) ;
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*15 - 3 * 10) / 4.0;
    CGFloat cViewHeight = 10 * 2 + width * row + (row - 1) * 10;
    self.collectionViewHeight.constant = cViewHeight;
    
    [self.collectionView reloadData];
}
@end

